package cn.schoolwow.ams.module.block.form.api.flow;

import cn.schoolwow.ams.domain.block.form.FormBlockRequest;
import cn.schoolwow.ams.module.block.form.initial.domain.FormBlockContext;
import cn.schoolwow.ams.module.block.form.initial.domain.FormBlockOperationContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.Validate;

public class ExecuteFormBlockUserOperationFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        FormBlockContext formBlockContext = flowContext.checkInstanceData(FormBlockContext.class);
        FormBlockRequest formBlockRequest = flowContext.checkInstanceData(FormBlockRequest.class);

        FormBlockOperationContext formBlockOperationContext = QueryInstanceList.newQuery(formBlockContext.blockOperationContextList)
                .addQuery("label", formBlockRequest.label)
                .execute()
                .getOne();
        Validate.notNull(formBlockOperationContext, "当前表单未配置操作按钮执行逻辑!按钮名称:"+formBlockRequest.label);

        JSONObject result = formBlockOperationContext.formBlockOperationHandler.handle(formBlockRequest);
        flowContext.putReturnData("result", result);
    }

    @Override
    public String name() {
        return "执行FormBlock的用户操作逻辑";
    }

}
